package jdbc04;

import org.junit.Test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JDBCUtilsDemo {
@Test
    public void testSelect() throws Exception {
    //1.通过工具类获取一个连接
    Connection connection = JDBCUtils.getConnection();
    System.out.println(connection);
    //通过PreparedStatement预编译sql并执行
    PreparedStatement ps = connection.prepareStatement("SELECT  * FROM test.product where price>?and price<?");
    ps.setDouble(1,1000);
    ps.setDouble(2,6000);
    ResultSet rs = ps.executeQuery();
    while (rs.next()){
        System.out.println(rs.getInt("pid") +" "+
                rs.getString("pname") +" "+
                rs.getDouble("price"));
    }
JDBCUtils.release(connection,ps,rs);
}
@Test
public void testUpdate() throws Exception {
    //1.通过工具类获取一个连接
    Connection connection = JDBCUtils.getConnection();
    System.out.println(connection);
    //通过PreparedStatement预编译sql并执行
    PreparedStatement ps = connection.prepareStatement("update  test.product  set pname=? where pid=?");
    ps.setString(1,"联想拯救者Y9000P");
    ps.setInt(2,1);
    int i = ps.executeUpdate();
    System.out.println(i);
    JDBCUtils.release(connection,ps,null);
}
}
